<template>
  <div v-if="props.imported">
    <div class="tips">本批次共导入{{props.allRecords}}条，成功{{succSize}}条，失败{{errorSize}}条，请点击下载批量打款失败Excel文件。</div>
    <ElTableWithColumns :height="150" :data="dataList" :columns="columns" :operateColProps="{ width: 120, fixed: false }" :keyId="props.keyId"> </ElTableWithColumns>
    <div style="display: flex;justify-content: center;margin-top: 10px;">
      <el-space>
        <el-button v-if="props.success" @click="addBill" type="primary">提交</el-button>
        <el-button v-else @click="$emit('onClose')">关闭</el-button>
      </el-space>
    </div>
  </div>
</template>

<script setup lang="ts">

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const sucList = ref([]);

// const tableData = ref([])
interface Props {
    /**
     * 导入结果
    */
    success: boolean;
    allRecords:number;
    params:object;
    /**
     * 是否导入
    */
    imported: boolean;
    list: any[];
    keyId:string;
}


const props = withDefaults(defineProps<Props>(), {
    success: false,
    imported: false,
    params:{},
    keyId:'',
    allRecords:0,
    list: () => []
});

 const emits = defineEmits(['onSubmit','onClose'])
const errorSize = computed(() => {
    if (props.success) {
      return props.allRecords - props.list.length ;
    }else{
      return props.list.length;
    }
})


const succSize = computed(() => {
    if (props.success) {
      return props.list.length;
    }else{
      return  props.allRecords - props.list.length;
    }
})

const columns = computed(() => {
    if (props.success) {
        return [
            {
                title: '发放金额',
                dataIndex: 'sumMoneyDecimal',
            },
            {
                title: '打款笔数',
                dataIndex: 'billIds',
            },
            {
                title: '平均金额（元）',
                dataIndex: 'avgDec',
            },
            {
                title: '最低金额（元）',
                dataIndex: 'minDec',
            },
            {
                title: '最高金额（元）',
                dataIndex: 'maxDec',
            },
            {
                title: '企业支付金额（元）',
                dataIndex: 'qyzf',
                renderText:function(row:any){
                  return proxy?.convertFenToYuan(row.qyzf)
                }
            },
            {
              title: '个税',
              dataIndex: 'raise',
              width:120,
            },
            {
                title: '服务费（元）',
                dataIndex: 'serviceChargeMoney',
                renderText:function(row:any){
                  return proxy?.convertFenToYuan(row.serviceChargeMoney)
                }
            },
            {
                title: '自由职业者实得金额（元）',
                dataIndex: 'amount',
                renderText:function(row:any){
                  return proxy?.convertFenToYuan(row.amount)
                }
            },
        ]
    }
    return  [
            {
                title: '姓名',
                dataIndex: 'workerName',
            },
            {
                title: '身份证号',
                dataIndex: 'cardId',
            },
            {
                title: '发放金额（元）',
                dataIndex: 'moneyDecimal',
            },
            {
                title: '备注',
                dataIndex: 'errorMsg',
                renderText:function(row:any){
                  return "<span style='color:red;'>"+row.errorMsg+"</span>";
                }
            },
        ]
})


//获取提交成功的LIst
const getSuccessList = ()=>{

  let list = props.list;
  let item = {
      billIds: list.length
  }
  let max = 0;
  let min = 0;
  let sumMoneyDecimal:number = 0; //发放金额,元
  let sumQyzf = 0 ;//企业支付，分
  let sumFwf = 0 ;//服务费,分
  let sumSd = 0 ;//实际得到，分

  list.map((it,idx)=>{
      if(idx == 0){
        max = parseFloat(it.moneyDecimal);
        min = parseFloat(it.moneyDecimal);
      }
      sumMoneyDecimal = parseFloat(sumMoneyDecimal)+ parseFloat(it.moneyDecimal);
      if(max <= parseFloat(it.moneyDecimal)){
        max = parseFloat(it.moneyDecimal).toFixed(2);
      }
      if(min >= parseFloat(it.moneyDecimal)){
        min = parseFloat(it.moneyDecimal).toFixed(2);
      }
      sumQyzf += it.qyzf;
      sumFwf +=  it.serviceChargeMoney;
      sumSd += it.amount
  })
  item.maxDec = max;
  item.minDec = min;
  item.avgDec =  parseFloat(sumMoneyDecimal/item.billIds).toFixed(2);
  item.qyzf = sumQyzf;
  item.serviceChargeMoney = sumFwf;
  item.sumMoneyDecimal = sumMoneyDecimal;
  item.amount = sumSd;
  return [item];
}


const addBill = ()=>{
  emits('onSubmit',{});
}

const dataList = computed(() => {
  if (props.success) {
    return getSuccessList();
  }else{
    return props.list;
  }
})

</script>

<style></style>
